package com.wangjiang.foundation.common.util.bcrytpt;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;

/**
 * @description: aes对称加密工具类
 * @author: wangjiang
 * @create: 2020-03-06 15:00
 */
public class AesUtils {

    //生成AES秘钥，然后Base64编码
    public static String genKeyAES() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey key = keyGen.generateKey();
        String base64Str = byte2Base64(key.getEncoded());
        return base64Str;
    }

    //将Base64编码后的AES秘钥转换成SecretKey对象
    public static SecretKey loadKeyAES(String base64Key) throws Exception {
        byte[] bytes = base642Byte(base64Key);
        SecretKeySpec key = new SecretKeySpec(bytes, "AES");
        return key;
    }

    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes) {
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }

    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }

    //加密
    public static byte[] encryptAES(byte[] source, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(source);
    }

    //解密
    public static byte[] decryptAES(byte[] source, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(source);
    }

    public static void main(String[] args) {
        try {
            // 身份证号码
            String idCard = "320520197104071954";
            System.out.println("加密前的明文: " + idCard);
            //生成AES秘钥，并Base64编码
//            String base64Str = genKeyAES();
            String base64Str = "gXEbHxPa0KJhAZnwhcWTxQ==";
//            System.out.println("AES秘钥Base64编码:" + base64Str);
            //将Base64编码的字符串，转换成AES秘钥

            //===================加密================

            //将Base64编码的字符串，转换成AES秘钥
            SecretKey aesKey = loadKeyAES(base64Str);
            //加密
            byte[] encryptAES = encryptAES(idCard.getBytes(), aesKey);
            //加密后的内容Base64编码
            String byte2Base64 = byte2Base64(encryptAES);
            System.out.println("加密并Base64编码的结果：" + byte2Base64);

            //===================解密================

            //将Base64编码的字符串，转换成AES秘钥
            SecretKey aesKey2 = loadKeyAES(base64Str);
            //加密后的内容Base64解码
            byte[] base642Byte = base642Byte(byte2Base64);
            //解密
            byte[] decryptAES = decryptAES(base642Byte, aesKey2);
            //解密后的明文
            System.out.println("解密后的明文: " + new String(decryptAES));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
